#include "View.h"

View::View(void) {
	window=NULL;
	screenSurface=NULL;
	renderer=NULL;
	this->pixelWindowWidth = StageParser::getInstance()->getGameStageData().getPxWindowWidth();
	this->pixelWindowHeight = StageParser::getInstance()->getGameStageData().getPxWindowHigh();

	this->scaleX = StageParser::getInstance()->getGameStageData().getPxWindowWidth() / StageParser::getInstance()->getGameStageData().getLUWindowWidth();
	this->scaleY = StageParser::getInstance()->getGameStageData().getPxWindowHigh() / StageParser::getInstance()->getGameStageData().getLUWindowHigh();
}

bool View::createWindow(void) {	
	bool success = true;

	if (SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		Log::getInstance()->write(Log::ERROR, SDL_GetError());
		return false;
	}
	else
	{
		return initWindow();
	}
}

bool View::initWindow() {
	window = SDL_CreateWindow("Worms FIUBA", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, this->getPixelWindowWidth(), this->getPixelWindowHeight(), SDL_WINDOW_SHOWN);
	if (window == NULL)
	{
		Log::getInstance()->write(Log::ERROR, SDL_GetError());
		return false;
	}
	else
	{
		return initRenderer();
	}
}

bool View::initRenderer() {
	renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
	if (renderer == NULL)
	{
		Log::getInstance()->write(Log::ERROR, SDL_GetError());
		return false;
	}
	else {
		SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
		
		return initPNGLoading();
	}
}

bool View::initPNGLoading() {
	int imgFlags = IMG_INIT_PNG|IMG_INIT_JPG|IMG_INIT_TIF;
	if (!(IMG_Init(imgFlags) & imgFlags))
	{
		Log::getInstance()->write(Log::ERROR, IMG_GetError());
		return false;
	}
	else
	{
		this->screenSurface = SDL_GetWindowSurface(window);
		return true;
	}
}

SDL_Texture* View::loadTexture(string path)
{
	SDL_Texture* newTexture = NULL;
	SDL_Surface* loadedSurface = IMG_Load(path.c_str());

	if (loadedSurface == NULL)
	{
		Log::getInstance()->write(Log::ERROR, IMG_GetError());
	}
	else
	{
		newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
		if (newTexture == NULL)
		{
			Log::getInstance()->write(Log::ERROR, SDL_GetError());
		}

		//Get rid of old loaded surface
		SDL_FreeSurface(loadedSurface);
	}

	return newTexture;
}

SDL_Surface* View::loadSurface(string path)
{
	//The final optimized image
	SDL_Surface* optimizedSurface = NULL;

	//Load image at specified path
	SDL_Surface* loadedSurface = IMG_Load(path.c_str());
	if (loadedSurface == NULL)
	{
		Log::getInstance()->write(Log::ERROR, SDL_GetError());
	}
	else
	{
		//Convert surface to screen format
		optimizedSurface = SDL_ConvertSurface(loadedSurface, getScreenSurface()->format, NULL);
		if (optimizedSurface == NULL)
		{
			Log::getInstance()->write(Log::ERROR, SDL_GetError());
		}

		//Get rid of old loaded surface
		SDL_FreeSurface(loadedSurface);
	}
	
	return optimizedSurface;
}

void View::setMasterView(View* view) {
	this->setWindow(view->getWindow());
	this->setRenderer(view->getRenderer());
	this->setScreenSurface(view->getScreenSurface());
}

SDL_Window* View::getWindow(){
	return window;
}
void View::setWindow(SDL_Window* window){
	this->window = window;
}

SDL_Surface* View::getScreenSurface() {
	return screenSurface;
}

void View::setScreenSurface(SDL_Surface* surface) {
	this->screenSurface = surface;
}

SDL_Renderer* View::getRenderer() {
	return this->renderer;
}

void View::setRenderer(SDL_Renderer* renderer) {
	this->renderer = renderer;
}

int View::getPixelWindowWidth() {
	return this->pixelWindowWidth;
}

void View::setPixelWindowWidth(int pixelWindowWidth) {
	this->pixelWindowWidth = pixelWindowWidth;
}

int View::getPixelWindowHeight() {
	return this->pixelWindowHeight;
}

void View::setPixelWindowHeight(int pixelWindowHeight) {
	this->pixelWindowHeight = pixelWindowHeight;
}

void View::close(void) {
	// Destruye la ventana
	SDL_DestroyRenderer(getRenderer());
	SDL_DestroyWindow(getWindow());
	setWindow(NULL);
	setRenderer(NULL);

	// Destruye componentes SDL
	IMG_Quit();
	SDL_Quit();
}